package cn.itsource.pethome.basic.service.impl;

import cn.itsource.pethome.basic.mapper.BaseMapper;
import cn.itsource.pethome.basic.query.BaseQuery;
import cn.itsource.pethome.basic.service.IBaseService;
import cn.itsource.pethome.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;
//                            |___EmployeeMapper
//                                    |___EmployeeProxyMapperImpl(Spring底层创建的)
//
//                            |___DepartmentMapper
//                                    |___DepartmentProxyMapperImpl(Spring底层创建的)
//                           由于Spring底层采用代理模式来实现接口的，如果不加泛型，它根本不确定应该是哪个实现类注入到BaseMapper接口中
    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }



    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    @Transactional
    public void batchRemove(List<T> ts) {
        baseMapper.batchRemove(ts);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> loadPageByQuery(BaseQuery query) {
        Long count = baseMapper.loadCountByQuery(query);
        if(count.equals(0L)){
            return new PageList<T>();
        }
        List<T> result = baseMapper.loadByQuery(query);
        return new PageList<T>(count, result);
    }


}
